/********************************************************************
Name: Sensitivity to Control Group Size, Synthetic Control (Figure A3)
Author: Dan Thompson
Date: September 2021
Description: 

********************************************************************/

clear all
set matsize 11000
set maxvar 15000
gl path = "~/Dropbox/Mariel Effects"

* Set up the output matrix
matrix define Output = J(500, 2, .)

* Loop over the control group size
local row = 0
qui forval i=25(10)1605 {

	* Bring in the county level analysis data
	use "$path/Replication/data/county_analysis_data.dta", clear
	keep if year<=1980
	tsset rank year
	
	* Limit the size of the control group
	keep if rank<=`i'
		
	* Create the synthetic control
	cap synth rep_vs rep_vs(1960) rep_vs(1964) rep_vs(1968) rep_vs(1972) rep_vs(1976), ///
		trunit(1) trperiod(1980)
	if _rc!=0 {
		noi di as error "synth with `i' counties error (max 3055) $S_TIME"
		continue
	}
	
	* Compute and store the effect
	matrix Treat = e(Y_treated)
	matrix Synth = e(Y_synthetic)
	local ++row
	matrix Output[`row',1] = `i'
	matrix Output[`row',2] = Treat[6,1] - Synth[6,1]
	
	* Print a status update
	noi di as text "synth with `i' counties complete (max 3055) $S_TIME"
}

* Call the synthetic control method output back into memory and save it
clear
svmat Output
rename (Output1 Output2) (num_counties effect)
keep num_counties effect
keep if effect!=.
save "$path/Replication/modified_data/synth_sensitivity.dta", replace

* Make the sensitivity plot (Fig A3)
use "$path/Replication/modified_data/synth_sensitivity.dta", clear
replace effect = effect*100
twoway (line effect num_counties, lc(gs2)), ///
	 xti("Number of Counties Included") ///
	 yti("Estimated Treatment Effect (% Points)") ///
	 ylab(0(2)10) ysc(r(0 10)) ///
	 xli(250, lc(gs6) lp(dot)) ///
	 xli(550, lc(gs6) lp(dot)) ///
	 graphregion(color(white)) ///
	 name("synth_sensitivity", replace)
graph export "$path/Replication/output/synth_sensitivity.pdf", replace name("synth_sensitivity")
